package com.oracle.lingshangejia.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.Gson;
import com.oracle.lingshangejia.Constant;
import com.oracle.lingshangejia.R;
import com.oracle.lingshangejia.adapter.ServicePackageDetailAdapter;
import com.oracle.lingshangejia.bean.DataBean;
import com.oracle.lingshangejia.bean.ServicePackageDetailBean;
import com.oracle.lingshangejia.mvp.base.BaseActivity;
import com.oracle.lingshangejia.utils.MyToast;
import com.oracle.lingshangejia.utils.OkHttpUtils;
import com.oracle.lingshangejia.utils.UIUtils;
import com.oracle.lingshangejia.view.TitleLayout;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

//定制服务包详情
public class ServicePackageDetailActivity extends BaseActivity {


    @BindView(R.id.title)
    TitleLayout mTitle;
    @BindView(R.id.rv)
    RecyclerView mRv;
    @BindView(R.id.et_add)
    EditText mEtAdd;
    @BindView(R.id.btn)
    Button mBtn;
    private int mId;
    private List<ServicePackageDetailBean.DataBean.DzbUserRecordDOSBean> mBeanList;
    private ServicePackageDetailAdapter mAdapter;
    private ServicePackageDetailBean mServicePackageDetailBean;

    @Override
    public int setLayoutId() {
        return R.layout.activity_service_package_detail;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        UIUtils.setStatusBarColor(getResources().getColor(R.color.colorBlue),this );

    }

    @Override
    public void initData() {
        super.initData();
        ButterKnife.bind(this);
        mTitle.setPadding(0,0 , 0, 0);
        mId = getIntent().getIntExtra("id", 0);
        intiRecyclerView();
        getDate();
    }

    private void intiRecyclerView() {
        mRv.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new ServicePackageDetailAdapter(mBeanList);
        mRv.setAdapter(mAdapter);
    }

    private void getDate(){
        HashMap<Object,Object> map = new HashMap<>();
        map.put("dzbId",mId);
        OkHttpUtils.getNetWork(this, Constant.servicePackageDetail,map ,handler );
    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {

            switch (msg.what){
                case 1:
                    Gson gson = new Gson();
                    String json = (String)msg.obj;
                    ServicePackageDetailBean bean = gson.fromJson(json, ServicePackageDetailBean.class);
                    if (bean.getCode() == 0){
                        mServicePackageDetailBean = bean;
                        mBeanList = bean.getData().getDzbUserRecordDOS();
                        if (mBeanList.size() >0){
                            mAdapter.replaceData(mBeanList);
                            mTitle.setText(mServicePackageDetailBean.getData().getName());
                        }else {
                            MyToast.show("没有评估内容");
                        }
                    }else {
                        MyToast.show(bean.getMsg());
                    }


                    break;
                case 2: //连接上服务器，错误
                    String error= (String) msg.obj;
                    Toast.makeText(ServicePackageDetailActivity.this,error,Toast.LENGTH_SHORT).show();
                    break;
                case 3:  //没有网络
                    Toast.makeText(ServicePackageDetailActivity.this,"网络不可用",Toast.LENGTH_SHORT).show();
                    break;
                case 4: //没有连接上服务器
                    Toast.makeText(ServicePackageDetailActivity.this,"连接服务器失败",Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };

    @OnClick(R.id.btn)
    public void onViewClicked() {
            send();
    }

    private void send(){
       String content = mEtAdd.getText().toString();
        if (TextUtils.isEmpty(content)) {
            MyToast.show("内容不能为空");
        } else {
            HashMap<Object,Object> map = new HashMap<>();
            map.put("dguId",mServicePackageDetailBean.getData().getId());
            map.put("content", content);
            OkHttpUtils.getNetWork(this, Constant.addServicePackageEvaluate , map, addhandler);
        }
    }

    @SuppressLint("HandlerLeak")
    private Handler addhandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {

            switch (msg.what){
                case 1:
                    Gson gson = new Gson();
                    String json = (String)msg.obj;
                    DataBean dataBean = gson.fromJson(json,DataBean.class );
                    if (dataBean.getCode() == 0){
                       /* ServicePackageDetailBean.DataBean.DzbUserRecordDOSBean dzbUserRecordDOSBean = new ServicePackageDetailBean.DataBean.DzbUserRecordDOSBean();
                        dzbUserRecordDOSBean.setContent(mEtAdd.getText().toString());
                        dzbUserRecordDOSBean.setDate(TimeUtils.getTodayDateTime());
                        mAdapter.add(mBeanList.size(),dzbUserRecordDOSBean );*/
                        getDate();
                        mEtAdd.setText("" );
                        MyToast.show("发送成功");
                    }else {
                        MyToast.show("发送失败");
                    }


                    break;
                case 2: //连接上服务器，错误
                    String error= (String) msg.obj;
                    Toast.makeText(ServicePackageDetailActivity.this,error,Toast.LENGTH_SHORT).show();
                    break;
                case 3:  //没有网络
                    Toast.makeText(ServicePackageDetailActivity.this,"网络不可用",Toast.LENGTH_SHORT).show();
                    break;
                case 4: //没有连接上服务器
                    Toast.makeText(ServicePackageDetailActivity.this,"连接服务器失败",Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };



}
